package niumao.minecraft.uhc.listener;

import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.player.PlayerShearEntityEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;

import java.util.Random;

public class Buff implements Listener {
    private static final ItemStack apple = new ItemStack(Material.APPLE, 1);

    public static void add(Player player, int time) {
        player.addPotionEffect(new PotionEffect(PotionEffectType.SATURATION, time * 60 * 20, 0), true);
        player.addPotionEffect(new PotionEffect(PotionEffectType.ABSORPTION, time * 60 * 20, 2), true);
        player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, time * 60 * 20, 0), true);
    }

    @EventHandler
    public void on(BlockBreakEvent event) {
        if (event.isCancelled()) return;
        Player player = event.getPlayer();
        if (player.getWorld().getName().equalsIgnoreCase("uhc")) {
            //树叶掉落苹果
            Block block = event.getBlock();
            if (block.getType() == Material.ACACIA_LEAVES
                    || block.getType() == Material.BIRCH_LEAVES
                    || block.getType() == Material.DARK_OAK_LEAVES
                    || block.getType() == Material.JUNGLE_LEAVES
                    || block.getType() == Material.SPRUCE_LEAVES
                    || block.getType() == Material.OAK_LEAVES) {
                Random rand = new Random();
                if (rand.nextInt(100) < 3) //百分之3的几率获得苹果
                    block.getWorld().dropItem(block.getLocation(), apple);
            }

            //给予急迫效果
            if (player.hasPotionEffect(PotionEffectType.FAST_DIGGING)) return;
            player.addPotionEffect(new PotionEffect(PotionEffectType.FAST_DIGGING, 50 * 20, 1), true);
        }
    }

    //剪羊毛,掉落线
    @EventHandler
    public void on2(PlayerShearEntityEvent event) {
        if (!event.getPlayer().getWorld().getName().equalsIgnoreCase("uhc")) return;
        if (event.getEntity().getType().equals(EntityType.SHEEP)) {
            World world = event.getEntity().getWorld();
            world.dropItem(event.getEntity().getLocation(), new ItemStack(Material.STRING, new Random().nextInt(2)));
        }
    }
}
